//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <string.h>
#include <elatypes.h>

int __cdecl strcmp(const char* str1, const char* str2)
{
    register int ret;

#ifdef _GNUC

#if defined(_x86)
    int temp0, temp1;
    ASM("1:"//str1-str2
        "lodsb;"//si(str1)->al
        "scasb;"//al-di(str2), CF would be set
        "jne 2f;"//if str1!=str2(ZF==0), goto 2:
        "testb %%al,%%al;"
        "jne 1b;"
        "xorl %%eax,%%eax;"//dest <-- dest xor src;(if di==si==al=='\0', eax=0)
        "jmp 3f;"
        "2:"
        "sbbl %%eax,%%eax;"//dest <-- dest-(src+CF)
        "orb $1,%%al;"//CF can specify "+" or "-"
        "3:"
        :"=a"(ret), "=S" (temp0), "=D" (temp1)
        :"1" (str1), "2" (str2));
    return ret;
#endif    //if defined(_x86)

//#if defined(_arm)
//    asm __volatile__(
//        "1:\tldr r2, [r0], #1\n\t"
//        "ldr r3, [r1], #1\n\t"
//        "cmp r3, r2\n\t"
//        "bne 2f\n\t"
//        "cmp r3, #0\n\t"
//        "bne 1b\n"
//        "2:\tsub r4, r2, r3\n\t"
//        "mov %0, r4\n"
//        :"=r"(ret)
//        ::"r2", "r3", "r4");
//        return ret;
//#endif    //if defined(_arm)

#endif    //ifdef _GNUC

    do {
        ret = (unsigned char)*str1 - (unsigned char)*str2;
        str1++;
    } while (ret == 0 && *str2++!= '\0');
    return ret;
}
